﻿var InitPostUrl = "../Handler/AdminHandler.ashx";
var YXQ = {
    Manager: {},
     Common: {}
}
YXQ.Common = {
    SelAll: function () {
        $("input:checkbox").each(function (i) {
            this.checked = true;
        });

    },
    NoSelAll: function () {
        $("input:checkbox").each(function (i) {
            this.checked = false;
        });
    },
 
    GetCheckboxItem: function () {
        var allSel = "";
        $("input:checkbox").each(function (i) {
            if (this.checked) {
                allSel += this.value + ",";
            }
        });
        return allSel.substring(0, allSel.length - 1);

    },
    GetOneItem: function () {
        var allSel = "";
        $("input:checkbox").each(function (i) {
            if (allSel == "") {
                allSel = this.value;
            }
        });

    },
    DelOneById: function (optype, id, rtnUrl) {
        YXQCore.Form.PostData(
            InitPostUrl,
            { OpType: optype, PostData: id },
            function (e) {
                if (e.ResultMessage == "Success") {
                    YXQ.Common.Message("成功删除！");
                    window.location.href = "../manager/" + rtnUrl;
                }
            });
    },
    CheckNull: function (ctrol, message) {
        if ($("[Datafield=" + ctrol + "]").length > 0) {
            if ($("[Datafield=" + ctrol + "]").val() == "") {
                YXQ.Common.Message(message);
                return false;
            }
        } else if ($("#" + ctrol).val() == "") {
            YXQ.Common.Message(message);
            return false;
        }

    },
    Message: function (msg) {
        $.messager.alert("温馨提示", msg);
        return;
    },
    UnBlock: function () {
        window.parent.location.reload();
        window.parent.unblock();
    },

    addTab: function (subtitle, url, icon) {
        if (!$('#tabs').tabs('exists', subtitle)) {
            $('#tabs').tabs('add', {
                title: subtitle,
                content: createFrame(url),
                closable: true,
                icon: icon
            });
        } else {
            $('#tabs').tabs('select', subtitle);
            YXQ.Common.refreshTab({ tabTitle: subtitle, url: url });
            //$('#mm-tabupdate').click();
        }
        
    },
    refreshTab: function (cfg) {
        var refresh_tab = cfg.tabTitle ? $('#tabs').tabs('getTab', cfg.tabTitle) : $('#tabs').tabs('getSelected');
        if (refresh_tab && refresh_tab.find('iframe').length > 0) {
            var _refresh_ifram = refresh_tab.find('iframe')[0];
            var refresh_url = cfg.url ? cfg.url : _refresh_ifram.src;

            _refresh_ifram.contentWindow.location.href = refresh_url;
        }
    },
    tabClose: function () {
        /*双击关闭TAB选项卡*/
        $(".tabs-inner").dblclick(function () {
            var subtitle = $(this).children(".tabs-closable").text();
            $('#tabs').tabs('close', subtitle);
        })
        /*为选项卡绑定右键*/
        $(".tabs-inner").bind('contextmenu', function (e) {
            $('#mm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });

            var subtitle = $(this).children(".tabs-closable").text();

            $('#mm').data("currtab", subtitle);
            $('#tabs').tabs('select', subtitle);
            return false;
        });
    }

}

YXQ.Manager = {
    Login: function () {
        var uname = $("#username").val();
        var password = $("#password").val();
        var chkUser = $("#chkUser").val();
        if (uname == "") {
            YXQ.Common.Message("对不起请输入用户名！");
            return;
        }
        if (password == "") {
            YXQ.Common.Message("对不起请输入密码！");
            return;
        }

        YXQCore.Form.PostData(
        InitPostUrl,
        { OpType: "ChkLogin", PostData: uname, strValue: password, chkUser: chkUser },
        function (e) {
             
            if (e.ResultMessage == "Failed") {
                 
                YXQ.Common.Message("对不起，用户名或密码错误！");
                return;
            } else if (e.ResultMessage == "SessionCodeFailed") {
                YXQ.Common.Message("对不起，验证码输入有误！");
                return;
            }  else if (e.ResultMessage == "Success") {
                window.location.href = "manager/index.aspx";
            }
        });

    },
    blockDialog: function (url, title, h) {
        var demo = $("#showDemoBox");
        var iframe = demo.find("#iframe");
        iframe.attr("src", url);
        demo.find("h3").text(title);
        iframe.attr("height", (h) + "px");
        $.blockUI({
            message: demo, css: {
                width: '1000px',
                left: ($(window).width() - 1000) / 2 + 'px',
                height: (h + 200) + "px"

            }
        });
         
    },
    CloseDialog:function(win){
        $("#"+win).dialog("close");
        if ($("#dg").length > 0) {
            $("#dg").datagrid("reload");
        }
    },
    RefreshDialog: function (win) {
        $("#" + win).dialog("refresh");
    },
    OpenDialog: function (win,url, tt,w,h) {
        
        $("#"+win).dialog({
            title: tt,
            href: url,
            width: w,
            height: h,
             modal: true,
            minimizable: true,
            maximizable:true
            
        });
        
        $("#"+win).dialog("open");
        //$('body>div.window>div.window-body').window('destroy');
        //  $("#openwin").dialog("refresh",url);
    },
    SaveProjectDetail: function (tp) {
        var tabDetail = $("[datafield='" + tp + "']");
        var jsonData = YXQCore.Form.GetDivFormData(tabDetail);
        //jsonData.id = $("[datafield='hfId']").val();
        jsonData.ProjectId = $("[datafield='hfId']").val();
        var jsonDataString = $.toJSON(jsonData);
        YXQCore.Form.PostData(InitPostUrl,
                    { OpType: "SaveProject", PostData: jsonDataString,StrValue:tp },
                    function (data) {
                        if (data.ResultMessage == "Success") {
                            YXQ.Common.Message("恭喜你数据保存成功！");
                            YXQ.Manager.CloseDialog();
                            return;
                        } else {
                            YXQ.Common.Message("对不起，数据保存失败！");
                            return;
                        }
                   });
    },
    CommonMessage: function (data) {

        if (data.ResultMessage == "Success") {
            YXQ.Common.Message("恭喜你数据保存成功！");
            YXQ.Manager.CloseDialog();
            $("#dg").datagrid("reload");
            return;
        } else {
            YXQ.Common.Message("对不起，数据保存失败！");
            $("#dg").datagrid("reload");
            return;
        }
    },


    SaveUser: function () {
      
        var tbDetail = $("#tbDetail");
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail);
        jsonData.Id = $("[datafield='hfId']").val(); 
        var jsonDataString = $.toJSON(jsonData);
        YXQCore.Form.PostData(InitPostUrl,
                    { OpType: "SaveUser", PostData: jsonDataString},
                    function (data) {
                        YXQ.Manager.CommonMessage(data);
                    });
    },
    SaveUserRight: function () {
        var tbDetail = $("#tbDetail");
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail);
        jsonData.UserId = $("[datafield='hfId']").val();
        var jsonDataString = $.toJSON(jsonData);
        YXQCore.Form.PostData(InitPostUrl,
                   { OpType: "SaveUserRight", PostData: jsonDataString },
                   function (data) {
                       YXQ.Manager.CommonMessage(data);
                   });
    },
    SaveDepart: function () {
        var tbDetail = $("#tbDetail");
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail); 
        var jsonDataString = $.toJSON(jsonData);
     
        YXQCore.Form.PostData(InitPostUrl,
                { OpType: "SaveDepart", PostData: jsonDataString },
                function (data) {
                    YXQ.Manager.CommonMessage(data);
                });
    },
    SaveMessage: function () {
        var tbDetail = $("#tbDetail");
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail);
        var jsonDataString = $.toJSON(jsonData);

        YXQCore.Form.PostData(InitPostUrl,
                { OpType: "SaveMessage", PostData: jsonDataString },
                function (data) {
                    YXQ.Manager.CommonMessage(data);
                });
    },
    GetDangan: function () {
        var tbDetail = $("#tbDetail");
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail);
        var jsonDataString = $.toJSON(jsonData);
        YXQCore.Form.PostData(InitPostUrl, { OpType: "SaveDa", PostData: jsonDataString }, function (data) {
            if (data.ResultMessage == "Success") {
                YXQ.Common.Message("数据提交成功");
                YXQ.Manager.CloseDialog("secwin");
            }
        });
    }
}